<template>
  <el-container>
    <my-theme-card>
      <el-header>资源管理</el-header>
      <el-main>
        <template>
          <el-col :span="1"><div class="grid-content bg-purple-light">
            <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addAnimationClick">新增</el-button>
          </div></el-col>
          <el-table :data="animationList" stripe border style="width: 100%">
            <el-table-column prop="name" label="名称" width="180" />
            <el-table-column label="图片" width="180">
              <template slot-scope="scope">
                <el-button v-if="!scope.row.thumbnails" type="text" size="small" @click="updateAnimationImageClick(scope.row)">+设置图片</el-button>
                <img v-for="item in scope.row.thumbnails" :key="item.id" :src="baseUrl+item.url" style="width:50px;height:50px;">
              </template>
            </el-table-column>
            <el-table-column label="星级">
              <template slot-scope="scope">
                <el-rate v-model="scope.row.level" disabled :max="7" />
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="180">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
              </template>
            </el-table-column>

          </el-table>
        </template>
      </el-main>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <my-delete-dialog
        :show-dialog="dialogConfirmVisible"
        :row="currentAnimationTemp"
        @deleteTask="deleteTask"
        @cancelTask="dialogConfirmVisible = false"
      />

      <el-dialog :title="form.sourceChooseTitle" :visible.sync="dialogSourcesVisible" :close-on-click-modal="false" center append-to-body>
        <source-list @chooseSourceHandle="closeSourceDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>
<script>
import request from '@/utils/request'
const sourceList = () => import('@/components/SourceList/index')
export default {
  components: {
    sourceList
  },
  data: function() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      form: {
        title: '添加徽章',
        sourceChooseTitle: '设置图片'
      },
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      currentAnimationTemp: {},
      animationList: [],
      dialogSourcesVisible: false,
      dialogConfirmVisible: false
    }
  },
  created() {
    this.query()
  },
  methods: {
    // 关闭
    closeSourceDialog: function(data) {
      if (data && data.length > 0) {
        this.chooseSourceTemp = []
        data.forEach((badges) => {
          this.chooseSourceTemp.push({
            id: badges
          })
        })
      }
      this.dialogSourcesVisible = false
      this.updateAnimationImageTask()
    },
    // 更新该徽章关联图片任务
    updateAnimationImageTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/animation/updateAnimationSources',
        method: 'post',
        data: {
          animation: this.currentAnimationTemp,
          sources: this.chooseSourceTemp
        }
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    // 设置徽章图片点击事件
    updateAnimationImageClick: function(animation) {
      this.currentAnimationTemp = animation
      this.dialogSourcesVisible = true
    },
    addAnimationClick: function() {
      this.$emit('toChangePage', 'add')
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    deleteClick: function(row) {
      this.currentAnimationTemp = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/animation/delete',
        method: 'post',
        data: this.currentAnimationTemp
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    query: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/animation/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.animationList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
